package com.yeyi.YTool;


import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

public class MerchantExporter extends ExporterAdapter<Merchant> {

	private HttpServletResponse response;

	public MerchantExporter(HttpServletResponse response) {
		this.response = response;
	}
	
	/**
	 * getHeader 各项所在下标
	 */
	private final int MERCHANT_CODE = 0;
	private final int MERCHANT_NAME = 1;
	private final int JOIN_PHONE = 2;
	private final int MERCHANT_TYPE = 3;
	private final int SHOP_NAME = 4;
	private final int OPERATE_TYPE = 5; // 经营方式
	private final int BRAND = 6;
	private final int SOURCE = 7;
	private final int AREA = 8;
	private final int CONTECT = 9; // 联系人
	private final int CONTECT_PHONE = 10;
	private final int OPERATE_STATUS = 11; // 运营状态
	private final int AUDIT_STATUS = 12; // 审核状态
	private final int APPLY_DATE = 13;

	@Override
	protected String[] getHeader() {
		return new String[] { "商户编码", "商户名称", "入驻手机号", "商户类型", "店铺名称", "经营方式", "品牌", "来源",
				"所在区域", "联系人", "联系电话", "运营状态", "审核状态", "申请时间"};
	}

	@Override
	protected String[] getSheetName() {
		return new String[] { "商户信息" };
	}

	@Override
	protected OutputStream getOutputStream() throws Exception {
		if (this.response != null) {
			response.setContentType("octets/stream");
			response.setCharacterEncoding("utf-8");
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(super.getFileName().getBytes(), "ISO8859-1"));
			OutputStream outputStream = response.getOutputStream();
			return outputStream;
		}
		return null;
	}

	@Override
	protected String getCellValue(Merchant data, int index) {
		if(null==data) {
			return "";
		}
		
		switch (index) {
		case MERCHANT_CODE:
			return data.getId()+"";
		case MERCHANT_NAME:
			return data.getName();
		case JOIN_PHONE:
			return data.getPhone();
		case SHOP_NAME:
			return data.getCompanyName();
		case MERCHANT_TYPE:
			if (data != null && data.getType()!=null) {
				// 1:4S 2:2S 3:加盟店 4：直营店 5：配件供应商 6:大圣科技
				if (data.getType() == 1) {
					return "4S";
				} else if (data.getType() == 2) {
					return "社区店";
				} else if (data.getType() == 3) {
					return "加盟店";
				} else if (data.getType() == 4) {
					return "直营店";
				} else if (data.getType() == 5) {
					return "汽车用品/配件";
				} else if (data.getType() == 6) {
					return "大圣科技";
				}else if(data.getType()==7){
					return "旗舰店";
				}else if(data.getType()==8){
					return "配件供应店";
				}
				return "";
			}
			break;
		case OPERATE_TYPE:
			if (data.getBusinessMode() != null){
				if(data.getBusinessMode() == 0){
					return "入驻";
				}else if(data.getBusinessMode() == 1){
					return "自营";
				}
			} else {
				return "";
			}
		case BRAND:
			return data.getBrandName();
		case SOURCE:
			if(0==data.getAuthStatus()) {
				return "导入";
			}
			return "注册";
		case AREA:
			return data.getAddress();
		case OPERATE_STATUS:
			// 1:启用 2：关闭 3:冻结
			if (data.getStatus() ==null) {
				return "";
			}
			if (data.getStatus() == 1) {
				return "启用";
			} else if (data.getStatus() == 2) {
				return "关闭";
			} else if (data.getStatus() == 3) {
				return "冻结";
			}
			return "";
		case CONTECT:
			return data.getPeople();
		case CONTECT_PHONE:
			return data.getContactPhone();
		case AUDIT_STATUS:
			// 是否审核通过 0:待完善 1：待审核 2:通过 3：不通过 4：复审
			if (data.getStatus() ==null) {
				return "";
			}
			if (data.getStatus() == 0) {
				return "待完善";
			} else if (data.getStatus() == 1) {
				return "待审核";
			} else if (data.getStatus() == 2) {
				return "审核通过";
			} else if (data.getStatus() == 3) {
				return "审核不通过";
			} else if (data.getStatus() == 4) {
				return "复审";
			}
			return "";
		case APPLY_DATE:
			if(null==data.getCreateTime()) {
				return "";
			}
			return data.getCreateTime();
		}
		
		return "";
	}

	// 继承工具类导出方法
	@Override
	public void export2(List<Merchant> data, String operationName) throws Exception {
		super.export2(data, operationName);
	}

	@Override
	protected String getBigHeader() {
		return "商户信息统计报表";
	}

}
